﻿using System;
using System.Collections.Generic;
using System.IO;

namespace PFB_TextFile_Read_Write
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "data.txt";
            CheckFile(fileName);

            string text = File.ReadAllText(fileName);

            List<Person> persons = new List<Person>();

            string[] personsText = text.Split('\n', '\r');

            for (int i = 0; i < personsText.Length; i++)
            {
                if(personsText[i] != String.Empty)
                {
                    Person person = Person.FromString(personsText[i]);
                    persons.Add(person);
                }
            }

            foreach (Person person in persons)
            {
                Console.WriteLine(person);
            }

            Person p = new Person();

            Console.WriteLine("Please input person first name and hit enter");
            p.FirstName = Console.ReadLine();

            Console.WriteLine("Please input person last name and hit enter");
            p.LastName = Console.ReadLine();

            Console.WriteLine("Please input person date of birth");
            p.DateOfBirth = DateTime.Parse(Console.ReadLine());

            File.AppendAllText(fileName, p.ToString() + Environment.NewLine);
            

            //string logFile = "log_" + DateTime.Now.Day.ToString();
            //if (File.Exists(logFile))
            //{
            //    File.Delete(logFile);
            //}
            //File.Create(logFile);
        }

        static void CheckFile(string path)
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }
        }
    }
}
